package com.course.masterservice.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.course.component.http.BasePageResponse;
import com.course.component.util.CopyUtil;
import com.course.component.util.DateUtils;
import com.course.component.util.SnowFlake;
import com.course.dto.reqs.RequestDictionaries;
import com.course.dto.reqs.RequestDictionariesInfo;
import com.course.dto.resp.ResMenu;
import com.course.entity.Category;
import com.course.entity.Dictionaries;
import com.course.entity.Permission;
import com.course.masterservice.DictionariesInfoMasterService;
import com.course.masterservice.DictionariesMasterService;
import com.course.service.DictionariesInfoService;
import com.course.service.DictionariesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;

import java.util.*;

/**
 * @Copyright: Copyright (c)  林中小鸟  All rights reserved.
 * @author: moon
 * @date: 2022/9/22 18:05
 * @Description: **********************
 */
@Service
public class DictionariesMasterServiceImpl implements DictionariesMasterService {

    @Autowired
    private DictionariesService dictionariesService;
    @Autowired
    private DictionariesInfoMasterService dictionariesInfoMasterService;
    @Override
    public BasePageResponse<Dictionaries> list(RequestDictionaries requestDictionaries) {
        return null;
    }

    @Transactional
    @Override
    public Boolean del(RequestDictionaries requestDictionaries) {
        Dictionaries delDictionaries = dictionariesService.getById(requestDictionaries.getId());
        boolean remove = dictionariesService.removeById(delDictionaries.getId());
        //删除字典下的dic详情
        RequestDictionariesInfo dictionaries = new RequestDictionariesInfo();
        dictionaries.setDic(delDictionaries.getId().toString());
        dictionariesInfoMasterService.del(dictionaries);
        //设置子类为本层
        dictionariesService.updatePid(delDictionaries.getPid(),requestDictionaries.getId());
        return remove;
    }

    @Override
    public Boolean update(RequestDictionaries requestDictionaries) {
        Dictionaries updateData = CopyUtil.copy(requestDictionaries,Dictionaries.class);
        if(ObjectUtils.isEmpty(updateData.getId())){
            updateData.setId(new SnowFlake().nextId());
        }
        return dictionariesService.saveOrUpdate(updateData);
    }

    @Override
    public Set<Dictionaries> tree(RequestDictionaries requestDictionaries) {
        List<Dictionaries> menuList = dictionariesService.list();
        return getMenuTree(menuList);
    }

    @Override
    public Dictionaries get(RequestDictionaries requestDictionaries) {
        return null;
    }

    //生成树
    private Set<Dictionaries> getMenuTree(List<Dictionaries> menuList){
        Set<Dictionaries> result = new HashSet<>();
        //list转map
        Map<Long, Dictionaries> nodeMap = new LinkedHashMap<>(menuList.size());
        for (Dictionaries treeNode : menuList) {
            nodeMap.put(treeNode.getId(), treeNode);
        }
        for (Dictionaries node : nodeMap.values()) {
            if (node.getPid() > 0) {
                Dictionaries parent = nodeMap.get(node.getPid());
                if (parent != null && !node.getId().equals(parent.getId())) {
                    parent.getChildren().add(node);
                    continue;
                }
            }
            result.add(node);
        }
        System.out.println(result);
        return result;
    }
}
